<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link href="../../resources/css/common/bootstrap3.min.css" rel="stylesheet" type="text/css" />
    <link href="../../resources/css/common/page.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../resources/css/common/component.css" />
</head>
<!--<link rel="import" href="/html/common/common_list.html" >-->
<div class="container body-content">
    <div id="demo">
        <div class="form-inline" style="margin:2% 0">
            <div class="form-group">
                <button type="button" class="btn btn-primary btn-sm" @click="add_role_dialg(0,'')">
                    添加
                </button>
            </div>
        </div>


        <div class="form-group">
            <table class="table table-bordered table-responsive table-striped">
                <thead>
                <tr>
                    <th v-for="(value, key) in gridColumns">
                        {{ key | capitalize }}
                    </th>
                    <th>操作</th>
                </tr>
                </thead>
                <tr v-for="entry in gridData">
                    <td v-for="(value, key) in gridColumns">
                        <div v-html="showHtml(entry[key],value)"></div>
                    </td>
                    <td>

                        <button type="button" class="btn btn-primary btn-sm" @click="add_role_dialg(entry['id'],entry['roleName'])">
                            权限设置
                        </button>
                        <button type="button" class="btn btn-danger btn-sm" @click="del_role(entry['id'])">
                            删除
                        </button>
                    </td>
                </tr>
            </table>
        </div>



        <div  class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">权限设置</h4>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" id="roleId"/>
                        <form>
                            <table align="center" width="500" height="200" border=0>
                                <tr   align="top" >
                                    <td>
                                        <label>角色名称:</label>
                                    </td>
                                    <td>
                                        <input v-model="roleName" type="text" placeholder="角色名称">
                                    </td>
                                    <td>

                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label>权限:</label>
                                    </td>
                                    <td>
                                        <table>

                                        <tr v-for="entry in permissionIds">
                                            <td>
                                            <input  v-bind:checked="entry.check" type="checkbox"  @click="checkout(entry)" >
                                            <input  v-bind:value="entry['permissionName']"  style="border:0;" readonly="readonly" >
                                            <br/>
                                            </td>
                                        </tr>

                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button"  class="btn btn-default" onclick="save_role()">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <hr />
    <footer>
    </footer>
</div>



<div id="app">
    <page></page>
</div>


</body>
<script type="text/javascript" src="../../resources/js/common/jquery-2.1.1.min.js"></script>
<script src="../../resources/js/common/bootstrap3.min.js" type="text/javascript"></script>
<script src="../../resources/js/common/bootstrap-datetimepicker.min.js"></script>
<script src="../../resources/js/common/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="../../resources/js/common/vue.min.js"></script>
<script src="../../resources/js/common/load_list.js"></script>
<script src="../../resources/js/common/common.js"></script>
<script>
    //默认数据
    var runUrl='/operator/query_roles';
    var param= {};
    var data={roleName:'',
        roleId:0,
        permissionIds:[]};
    //列表th信息
    data['th']={'id':'角色ID', 'roleName':'角色名称','updateTime':'修改时间'};
    //列表字段信息以及类型  类型 video vioce img a no 五种
    data['gridColumns'] = {'id':'no', 'roleName':'no','updateTime':'no'};

</script>

<script>

    function del_role(id){
        jQuery.get("/operator/del_role", {id: id}, function (data) {
            if(data.retCode == 1){
                alert("操作成功！！！");
                window.location.href="/html/operator/role.html";
            }else{
                alert("操作失败，请重试！！！");
            }
        });
    }


    function checkout(entry)
    {
        entry.check=!entry.check;
    }



    function add_role_dialg( roleId, roleName){

        demo.roleId=roleId;
        demo.roleName=roleName;

        var all_permissions=queryAllPermissionIds();
        var role_permission=queryPermissionByRoleId(roleId);
        for (var k = 0, all_permissions_length = all_permissions.length; k < all_permissions_length; k++) {
            all_permissions[k]['check']=false;
            for(var j = 0, length = role_permission.length; j < length; j++) {
                if(all_permissions[k]['id']==role_permission[j]['id']) {
                    all_permissions[k]['check'] = true;
                }
            }
        }
        demo.permissionIds=all_permissions;

        $('#myModal').modal({
            keyboard: true
        });
    }
    function save_role()
    {
        if(demo.roleName=="")
        {
            alert("角色名称不能为空");
            return ;
        }

        var param={};
        var permissionList=new Array();
        for (var k = 0, length = demo.permissionIds.length; k < length; k++) {
            if (demo.permissionIds[k]['check']) {
                permissionList.push(demo.permissionIds[k]['id']);
            }
        }
        param['id']=demo.roleId;
        param['tagCode']=demo.tagCode;
        param['roleName']=demo.roleName;
        param['permission']=permissionList.join(",");


        var data= executeAjaxByPost('/operator/add_role',param);
        if(data.retCode==1)
        {
            alert("保存成功");
            window.location.href = "/html/operator/role.html";
        }
        else
        {
            alert("保存失败"+data.retMsg);
            return;
        }
        return ;

    }


    function queryPermissionByRoleId(roleId) {
        var param={};
        param['roleId']=roleId
        var  data=executeAjaxByPost("/operator/query_permission_by_id",param);

        if(data.retCode==1)
        {
            return data.list;
        }
        return [];

    }
    function queryAllPermissionIds(){

       var  data=executeAjaxByPost("/operator/query_permission",[]);
       if(data.retCode==1)
       {
           return data.list;
       }
       else {
           return [];
       }

    }




</script>
</html>

